package com.htdata.iiot.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.htdata.iiot.api.pojo.Metric;

public class FileUtils {
	private static Logger logger = LogManager.getLogger(FileUtils.class);
	public static Map<String, String> metricValue = new HashMap<>();
	private static DecimalFormat fmt = new DecimalFormat("0.##");

	// 从resources下读取文件获取测点值范围
	public static void read(String fileName) {
		//InputStream inputStream = FileUtils.class.getResourceAsStream("conf/" + fileName);
		File fileConf = new File("conf/"+fileName);
        BufferedReader reader = null;
        FileInputStream fileInputStream = null;
        try {
            if(fileConf.exists()){
                fileInputStream = new FileInputStream(fileConf);
                logger.info("加载的配置文件名称为: "+ fileName);
                //InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "UTF-8");
                InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8");
                reader = new BufferedReader(inputStreamReader);
                String tempString = null;
                while ((tempString = reader.readLine()) != null) {
                    // 单柱移动立式车床SMVT630 纵向快移速度 50~100
                    String[] split = tempString.split("\\s+");
                    if (split.length == 4) {
                        String key = split[0].trim() + "_" + split[1].trim()+ "_" + split[2].trim();
                        metricValue.put(key, split[3].trim());
                        logger.info("读取文件key: "+key+"; 测点值范围: "+split[3].trim());
                    }
                }
            }
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (reader != null) {
				try {
					reader.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	public static Object getValue(Metric metric) {
		// 单柱移动立式车床SMVT630 纵向快移速度 50~100
		String value = metricValue.get(metric.getTid()+"_"+metric.getDeviceName()+"_"+metric.getComponentName());
		if (value != null) {
			String[] split = value.split("~");
			if (split.length == 1) {
				return fmt.format(Double.parseDouble(split[0]));
			} else {
				// 6 ~ 15 = 9 new Random().nextInt(9) + 6
				double min = Double.parseDouble(split[0]) * 1000;
				double max = Double.parseDouble(split[1]) * 1000;
				return fmt.format((new Random().nextInt((int) (max - min)) + min) / 1000);
			}
		}
		double val = 0.0;
		switch (metric.getName()) {
		case "status":
			val =1000;
			break;
		case "AirVelocity":// 空速度
			val =50;
			break;
		case "backSpeed":// 回速度
			val =45;
			break;
		case "CuttingTimes":// 每分钟剪切次数
			val =3 + new Random().nextInt(4);
			break;
		case "flywheelDropRate":// 飞轮降速率
			val =0;// 计算
			break;
		case "focalLength":// 透镜焦距
			val =125;
			break;
		case "highSpeed":// 高速
			val =1000;
			break;
		case "InitialSpeed":// 飞轮初始转速
			val =300;
			break;
		case "MaleWeighStrength":// 公称力
			val =50;
			break;
		case "pressure":// 辅助气体气压//折弯机压力//冲床气源气压

			if (metric.getDid() == 86869) {// 激光切割机
				val =0.85 + new Random().nextInt(5) * 0.01;
			} else if (metric.getDid() == 87626) {// 折弯机
				val =10 + new Random().nextInt(5) * 0.1;
			} else if (metric.getDid() == 87632) {// 剪板机
				// val =1200);
			} else if (metric.getDid() == 87636) {// 冲床
				val =1 + new Random().nextInt(5) * 0.01;
			}

			break;
		case "RearSpeed":// 飞轮降速后转速
			val =300;
			break;
		case "speed":// 切速
			val =6;
			break;
		case "temperature":// 油温
			val =35 + new Random().nextInt(25);
			break;
		case "thickness":// 钢板厚度
			val =2;
			break;
		case "workSpeed":// 工作速度
			val =9;
			break;

		default:
			val =Math.ceil(new Random().nextDouble() * 10000) * 0.01;
			break;
		}
		
		return val;
	}

	public static Object getValueRangeFromMetric(Metric metric) {
		if(metric.getValueMin() != 0.0 && metric.getValueMax() != 0.0 && metric.getValueMax() > metric.getValueMin()){
			Double d = (metric.getValueMax()-metric.getValueMin())*1000;
			double  val = metric.getValueMin()*1000+new Random().nextInt(d.intValue());// ((Math.sin(Math.toRadians(System.currentTimeMillis()))+1)*(metric.getValueMax() - metric.getValueMin())*1000);
			return fmt.format(val*0.001);
		}else if(metric.getValueMin() != 0.0 && (metric.getValueMax() == 0.0||metric.getValueMin()==metric.getValueMax())){
			return fmt.format(metric.getValueMin());
		}

		double val = 0.0;
		switch (metric.getName()) {
			case "status":
				val =1000;
				break;
			case "AirVelocity":// 空速度
				val =50;
				break;
			case "backSpeed":// 回速度
				val =45;
				break;
			case "CuttingTimes":// 每分钟剪切次数
				val =3 + new Random().nextInt(4);
				break;
			case "flywheelDropRate":// 飞轮降速率
				val =0;// 计算
				break;
			case "focalLength":// 透镜焦距
				val =125;
				break;
			case "highSpeed":// 高速
				val =1000;
				break;
			case "InitialSpeed":// 飞轮初始转速
				val =300;
				break;
			case "MaleWeighStrength":// 公称力
				val =50;
				break;
			case "pressure":// 辅助气体气压//折弯机压力//冲床气源气压
				if (metric.getDid() == 86869) {// 激光切割机
					val =0.85 + new Random().nextInt(5) * 0.01;
				} else if (metric.getDid() == 87626) {// 折弯机
					val =10 + new Random().nextInt(5) * 0.1;
				} else if (metric.getDid() == 87632) {// 剪板机
					// val =1200);
				} else if (metric.getDid() == 87636) {// 冲床
					val =1 + new Random().nextInt(5) * 0.01;
				}
				break;
			case "RearSpeed":// 飞轮降速后转速
				val =300;
				break;
			case "speed":// 切速
				val =6;
				break;
			case "temperature":// 油温
				val =35 + new Random().nextInt(25);
				break;
			case "thickness":// 钢板厚度
				val =2;
				break;
			case "workSpeed":// 工作速度
				val =9;
				break;
			default:
				//val =Math.ceil(new Random().nextDouble() * 10000) * 0.01;
				
				val = 60+new Random().nextInt(15)+(Math.sin(Math.toRadians(System.currentTimeMillis()))+1)*1000*0.001;
				break;
		}

		return val;
	}


}
